/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author BTC
 */
public final class FileWindow extends JFrame {

    private MainWindow mainWindow;
    private FileWindow fileWindow;
    private JProgressBar progressBar;
    private JButton importButton;
    private JButton exportButton;
    private JPanel progressBarsPanel;
    private List<JProgressBar> progressBars;

    private ImportHandler importHandler;
    private ExportHandler exportHandler;

    private JFileChooser fileChooser;

    public FileWindow(MainWindow mainWindow) {
        super("File Manager.");
        this.mainWindow = mainWindow;
        this.fileWindow = this;

        this.importHandler = new ImportHandler();
        this.exportHandler = new ExportHandler();
        this.fileChooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Coma separated values", "cvs");
        this.fileChooser.setFileFilter(filter);

        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        JPanel content = this.buildDisplay();
        this.setContentPane(content);
        this.pack();
        this.setVisible(true);
    }

    private JPanel buildDisplay() {
        JPanel display = new JPanel();
        display.setLayout(new BoxLayout(display, BoxLayout.Y_AXIS));
        JPanel row = new JPanel();
        this.importButton = new JButton("Import from file.");
        this.importButton.addActionListener(importHandler);
        row.add(importButton);
        this.exportButton = new JButton("Export to file.");
        this.exportButton.addActionListener(exportHandler);
        row.add(exportButton);
        display.add(row);
        this.progressBarsPanel = new JPanel();
        return display;
    }

    private class ImportHandler implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            int returnVal = fileChooser.showOpenDialog(FileWindow.this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                mainWindow.importFile(fileWindow, fileChooser.getSelectedFile().getAbsolutePath());
            }

        }

    }

    private class ExportHandler implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            int returnVal = fileChooser.showOpenDialog(FileWindow.this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                mainWindow.exportFile(fileWindow, fileChooser.getSelectedFile().getAbsolutePath());
            }

        }

    }
}
